package com.gyz.websocket.handler;

import com.gyz.websocket.message.SendResponse;
import com.gyz.websocket.message.SendToAllRequest;
import com.gyz.websocket.message.SendToUserRequest;
import com.gyz.websocket.util.WebSocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * 群聊消息处理器
 */
@Component
public class SendToAllHandler implements MessageHandlers<SendToAllRequest> {

    @Override
    public void execute(Session session, SendToAllRequest message) {
        // 这里，假装直接成功
        SendResponse sendResponse = new SendResponse();
        sendResponse.setMsgId(message.getMsgId());
        sendResponse.setCode(0);
        WebSocketUtil.send(session, SendResponse.TYPE, sendResponse);
        // 创建转发的消息
        SendToAllRequest sendToAllRequest = new SendToAllRequest();
        sendToAllRequest.setMsgId(message.getMsgId());
        sendToAllRequest.setContent(message.getContent());

        // 广播发送
        WebSocketUtil.broadcast(session, SendToUserRequest.TYPE, sendToAllRequest);
    }

    @Override
    public String getType() {
        return SendToAllRequest.TYPE;
    }

}
